﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;
using Commons.Data;
using Background.Core.Entity;
namespace Background.Core.DAL
{
    public class PKIDTableRepository : BackgroundRepositoryBase, IRepository
    {
        /// <summary>
        /// 生成PK最大ID自动加1主键方法
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int GetNewPKID(PKIDTable code) //由后后台也要建立用户，所以把它改为public -zhangzhanling 
        {
            string _code = null;
            int idNum = 0;
            using (TransactionScope transactionScope = new TransactionScope())
            {
                try
                {
                    _code = Enum.GetName(typeof(PKIDTable), code);
                    base._db.Database.ExecuteSqlCommand("UPDATE PkIdTable SET IDNum= IDNum+1 WHERE Code={0}", _code);
                    idNum = base.GetEntities<PkIdTable>(i => i.Code == _code).Max(j => j.IDNum);
                    transactionScope.Complete();
                    return idNum;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    transactionScope.Dispose();
                }
            }
        }
    }
}
